package com.xinsoft.controller.sale;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xinsoft.common.ApprovedParam;
import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.ApproveDTO;
import com.xinsoft.entity.dto.ScmSaleNoticeDTO;
import com.xinsoft.entity.dto.ScmSaleNoticeParam;
import com.xinsoft.entity.vo.ScmSaleNoticeListVo;
import com.xinsoft.entity.vo.ScmSaleNoticeVo;
import com.xinsoft.service.ApproveService;
import com.xinsoft.service.ScmSaleNoticeService;
import com.xinsoft.utils.ExcelUtils;
import com.xinsoft.utils.TokenUtil;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 云供应链/销售管理/发货通知
 */
@Slf4j
@RestController
@RequestMapping("/scm/saleNotice")
public class ScmSaleNoticeController {
    @Autowired
    private ScmSaleNoticeService saleNoticeService;
    @Autowired
    private ApproveService approveService;

    /**
     * 分页
     */
    @PostMapping("/pageList")
    public Response<IPage<ScmSaleNoticeListVo>> pageList(@RequestBody ScmSaleNoticeParam param) {
        log.info("发货通知分页查询参数：{}", JSON.toJSON(param));
        param.setCurrentUserId(TokenUtil.getUserId());
        return Response.succeed(saleNoticeService.findPageListByParam(param));
    }

    /**
     * 暂存
     */
    @PostMapping("/addOrUpdate")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> addOrUpdate(@RequestBody ScmSaleNoticeDTO saleNoticeDTO) {
        // 参数校验
        ValidatorUtils.validateEntity(saleNoticeDTO);
        saleNoticeService.tempSave(saleNoticeDTO);
        return Response.succeed();
    }

    /**
     * 详情
     *
     * @param id 发货通知id
     * @return {@link Response}<{@link ?}>
     */
    @GetMapping("/detail")
    public Response<ScmSaleNoticeVo> detail(@RequestParam(value = "id", defaultValue = "") Integer id) {
        ScmSaleNoticeVo vo = saleNoticeService.detail(id);
        // 审批记录
        vo.setScmApproveHisList(approveService.approvalHistory(id, Const.ApproveType.FHTZ));
        return Response.succeed(vo);
    }

    /**
     * 删除
     */
    @PostMapping("/del")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> del(@RequestBody List<Integer> ids) {
        List<Integer> newIds = ids.stream().distinct().collect(Collectors.toList());
        saleNoticeService.del(newIds);
        return Response.succeed();
    }

    /**
     * 导出
     */
    @PostMapping("/export")
    public Response<?> export(@RequestBody @ApiParam("查询参数") ScmSaleNoticeParam param, HttpServletResponse response) throws Exception {
        List<ScmSaleNoticeListVo> list = saleNoticeService.findListByParam(param);
        ExcelUtils.exportByEasypoi(list, ScmSaleNoticeListVo.class, "发货通知", "发货通知", response, "发货通知");
        return Response.succeed();
    }

    /**
     * 提交审批
     */
    @PostMapping("/submitApprove")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> submitApprove(@RequestBody ScmSaleNoticeDTO saleNoticeDTO){
        // 参数校验
        ValidatorUtils.validateEntity(saleNoticeDTO);
        Integer id = saleNoticeService.tempSave(saleNoticeDTO);
        ApproveDTO approveDTO = new ApproveDTO();
        approveDTO.setSourceId(id);
        approveDTO.setSourceType(Const.ApproveType.FHTZ);
        approveDTO.setSourceTypeName(Const.ApproveTypeName.FHTZ);
        return approveService.submitApprove(approveDTO);
    }

    /**
     * 审批
     */
    @PostMapping("/approved")
    public Response<?> approved(@Validated @RequestBody ApprovedParam param){
        return approveService.approved(param, Const.ApproveType.FHTZ);
    }

    /**
     * 审批流程
     */
    @GetMapping("/approveHis")
    public Response<?> approveHis(@RequestParam(value = "id") Integer id){
        return Response.succeed(approveService.approvalHistory(id, Const.ApproveType.FHTZ));
    }
}
